#! /usr/env/bin python
# gmail account username: twitterparser
# gmail account pass: threelittlepigs
#Email info:
#twitterparser@gmail.com
#pass: ireadtweets

#Twitter user info:
#username: ParsingTweets
#pass: ireadtweets

import sys
import hashlib
import string
import re
import httplib
import json
import socket
import urllib
import sqlite3
import datetime

class Ranker:
	def __init__(self, database = 'test.db'):
		self.dict = {}
		self.populate_dictionary()
		self.database = database

	"""
	Reads the file "words.txt" to determine words and associated values.
	"""
	def populate_dictionary(self):
		f = open('words.txt', 'r')
		raw = f.readlines()

		for lines in raw:
			self.add_entry(lines)

	"""
	Basic function used to help populate the dictionary
	"""
	def add_entry(self, lines):
		lines = lines.strip('\n')
		line = lines.split(' ')
		if len(line) == 2:
			self.dict[line[0]] = int(line[1])

	"""
	Self-explanatory
	"""
	def print_dict(self):#c.execute('''create table test(name text, followers int, date text)''')
		for entry in self.dict:
			print entry, self.dict[entry]

	"""
	Calculates value of post solely based on words. Does not factor in number
	of followers. These words and values come from "words.txt"
	"""
	def calculate_post(self, content):
		newcontent = content.strip('\n')
		newcontent = newcontent.strip('#.;[]{}()1234567890!@$%^&*_+-|?/').lower()
		words = newcontent.split(' ')
		#print words
		value = 0
		for word in words:
			#print word
			if word in self.dict:
				value += self.dict[word]
		return value

	"""
	Finds the number of followers a user has given by the authorname value passed
	in the passDict dictionary for the twitter protocol.
	"""
	def twitter_follower_calc(self, passDict):
		c = httplib.HTTPConnection("api.twitter.com");
		params = {}
		params['screen_name'] = passDict['authorname']
		path = "%s?%s" %("/1/users/show.json", urllib.urlencode(params))
		conn = sqlite3.connect(self.database)
		cursor = conn.cursor()
		tmp = (hashlib.md5(passDict['content']).hexdigest(),)
		cursor.execute('select * FROM userdata WHERE hash=?', tmp)
		if cursor.fetchone() != None:
			cursor.close()
			return -1
		cursor.close()
		try:
			c.request('GET', path)
			data = c.getresponse().read()
			c.close()
			try:
				result = json.loads(data)
			except ValueError:
				return None
		except (httplib.HTTPException, socket.error, socket.timeout):
			print "Error searching, here is why: %s" %(e)
			return None
		try:
			return result['followers_count']
		except:
			return 126
		
	"""
	Finds the number of followers a user has given by the authorname value passed
	in the passDict dictionary for the livejournal protocol.
	"""
	def livejournal_follower_calc(self, passDict):
		url = "http://www."
		url = url + passDict['authorname'] + ".livejournal.com/profile"
		conn = sqlite3.connect(self.database)
		cursor = conn.cursor()
		tmp = (hashlib.md5(passDict['comment']).hexdigest(),)
		cursor.execute('select * from userdata where hash=?', tmp)
		if cursor.fetchone() != None:
			cursor.close()
			return -1
		cursor.close()
		read = False
		while read == False:
			try:
				page = urllib.urlopen(url)
				read = True
			except IOError:
				pass
		page = page.read()
		regex = "Friends " 
		lindex = string.find(page, "Friends (")
		rindex = string.find(page, ")", lindex)
		i = lindex + 8
		friends = ""
		while i < rindex - 1:
			i = i + 1
			friends = friends + page[i]
		return string.atoi(friends)

	"""
	Determines the overall value of a post by finding the product of the number of
	followers a user has and the post score (calculated by supplied words and
	associated values).
	"""
	def calculate_value(self, passDict):
		#authorid, authorname, content, url, source
		if passDict['source'] == "twitter":
			postval = self.calculate_post(passDict['content'])
			followers = self.twitter_follower_calc(passDict)
		elif passDict['source'] == "livejournal":
			postval = self.calculate_post(passDict['comment'])
			followers = self.livejournal_follower_calc(passDict)
		else:
			print "Unknown source"
			return -1, -1

		if followers == -1:
			return -1, -1
		#return followers
		return postval, followers

def parser_test():
	parse = Parser()
	parse.print_dict()
